package org.kim.service;

import org.kim.core.Session;
import org.kim.dao.AccountDao;
import org.kim.pojo.Account;
import org.kim.utils.Md5Utils;

import java.util.Optional;

public class AccountService {
    AccountDao accountDao=new AccountDao();

    public boolean accountExists(String accountId) {
        return accountDao.accountExists(accountId);
    }

    public boolean register(Account account) throws Exception {
        account.setPassword(Md5Utils.getMd5(account.getPassword()));
        return accountDao.register(account);
    }

    public Account login(Account inputAccount) {
        Account account = accountDao.search(inputAccount.getAccountId());
        if (account != null) {
            if(account.getPassword().equals(Md5Utils.getMd5(inputAccount.getPassword()))){
                inputAccount.setPassword("");
                inputAccount.setName(account.getName());
                inputAccount.setRole(account.getRole());
                Session.clear();
                Session.set(inputAccount);
                return account;
            };
        }
        return null;
    }
}
